﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.MultiResource;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [AllowAnonymous]
    public class AccountController : Controller
    {
        //
        // GET: /AdminAccount/

        #region Manager Account
        //
        //GET: /Admin/LogIn
        [HttpGet]
        public ActionResult Login()
        {
            if (Request.IsAjaxRequest())
            {
                return PartialView("Login");
            }
            return View("Login");
        }

        [HttpPost]
        public ActionResult Login(LogInModel model,string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var db = new CMSMVC4Entities();
                    var accounts = db.Accounts.FirstOrDefault(x => x.Account1.Equals(model.Account));
                    ////----------------------------------------------------------------------------------------
                    if (accounts != null)
                    {
                        accounts =
                            db.Accounts.FirstOrDefault(
                                x => x.Password.Equals(model.Password) && x.Account1.Equals(model.Account));
                        if (accounts != null)
                        {
                            FormsAuthentication.SetAuthCookie(model.Account, true);
                            if (Url.IsLocalUrl(returnUrl))
                            {
                                if (Request.IsAjaxRequest())
                                {
                                    return JavaScript("window.location = '" + returnUrl + "'");
                                }
                                return Redirect(returnUrl);
                            }
                            if (Request.IsAjaxRequest())
                            {
                                return JavaScript("window.location = '/Admin/Home/Index'");
                            }
                            return RedirectToAction("Index", "Home", new { area = "Admin" });
                        }
                        ModelState.AddModelError("Password", Resource.Pasword_Error);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", Resource.Account_Error);
                    }
                }
                if (Request.IsAjaxRequest())
                {
                    return PartialView(model);
                }
                ViewBag.Title = Resource.Title_Login;
                return View(model);
            }
            catch (Exception e)
            {
                var log = log4net.LogManager.GetLogger(GetType());
                log.Info(e.Message);
                return View("Error");
            }
        }

        //
        // GET: /Admin/LogOut
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login", "Account", new { area = "Admin" });
        }


        //
        //GET: Admin/ChangePassword
        [HttpGet]
        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                var user = User.Identity.Name;
                var db = new CMSMVC4Entities();
                var data = db.Accounts.FirstOrDefault(x => x.Account1 == user && x.Password.Equals(model.Pasword));
                if (data != null)
                {
                    data.Password = model.NewPassword;
                    db.SaveChanges();
                    ModelState.AddModelError("","Đã đổi mật khẩu thành công");
                    return View(model);
                }
                ModelState.AddModelError("Pasword", "Sai Mật Khẩu");
            }
            return View(model);
        }
        #endregion
    }
}
